/*
 * @Author: evan.wan
 * @Date:   2016-07-02 15:55:20
 */

'use strict';
define([
    '../../../common/service',
    'text!../tpl/addElementDialog.html'
], function(
    http,
    tpl
) {
    function me() {
        var self = this;
        this.init = function(templateId, callback) {
            self.templateId = templateId;
            self.callback = callback;
            self.$root = $('#add-element-dialog');
            self.render();
        }

        this.render = function() {
            self.$root.html(I18N.parse(tpl, 'templateDetail'));
            self.event();
            self.$root.modal('show');
        }

        this.event = function() {
            self.$root.find('.btn-ok').off('click').on('click', $.proxy(self.confirm, self));

        }

        this.confirm = function() {
            $.ajaxFileUpload({
                url: apiUrl + "/ecelement/upload?templateId="+self.templateId,
                fileElementId: 'file',
                type:'post',
                secureuri: false,
                dataType:'JSON',
                success: function (res, status) {
                    self.callback();
                    self.$root.modal('hide');
                }
            });

        }

    }
    return me;
});
